
	<div class="ibox float-e-margins">
		<div class="ibox-title" style="border: none;">
			<h5 style="color: #00afef"><i>Add</i></h5>
				<div class="ibox-tools add">
					<i id="add_loading_icon" style="color: gray; display: none;" class="fa fa-spinner fa-spin fa-lg"></i>&nbsp;
					<button id="save_glbusinessunit" class="btn btn-primary btn-sm save-btn-tab" type="">Save</button>
					<button id="cancel_add" class="btn-outline btn btn-primary btn-sm cancel-btn-tab" >Cancel</button>
				</div>
		</div>
		<div class="ibox-content">
			<form class="form-horizontal" id="glbuForm">
				<div class="form-group">
					<label class="col-lg-4 control-label">Code</label>
					<div class="col-lg-5">
						<input type="text" name="bu_code" class="form-control">
					</div>
				</div>
				<div class="form-group">
					<label class="col-lg-4 control-label">Name</label>
					<div class="col-lg-5">
						<input type="text" name="bu_name" class="form-control">
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-4 control-label">Sales Account</label>
					<div class="col-sm-5">
						<select class="form-control m-b chosen-select" data-placeholder="Choose . . " name="bu_sales">
							<option value="">--</option> 
							<?php foreach ($sales as $sl_acct) { ?>
                                <option value="<?php echo $sl_acct->account_id; ?>"><?php echo $sl_acct->acct_name; ?> (<?php echo $sl_acct->acct_code; ?>)</option>
                            <?php } ?>
						</select>
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-4 control-label">COGS Account</label>
					<div class="col-sm-5">
						<select class="form-control m-b chosen-select" data-placeholder="Choose . . " name="bu_cogs">
							<option value="">--</option>
                        <?php foreach ($cogs as $cogs_acct) { ?>
                            <option value="<?php echo $cogs_acct->account_id; ?>"><?php echo $cogs_acct->acct_name; ?> (<?php echo $cogs_acct->acct_code; ?>)</option>
                        <?php } ?>
						</select>
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-4 control-label">Expense Account</label>
					<div class="col-sm-5">
						<select class="form-control m-b chosen-select" data-placeholder="Choose . . " name="bu_exp">
							<option value="">--</option>
							<?php foreach ($expense as $exp_acct) { ?>
                                <option value="<?php echo $exp_acct->account_id; ?>"><?php echo $exp_acct->acct_name; ?> (<?php echo $exp_acct->acct_code; ?>)</option>
                            <?php } ?>
						</select>
					</div>
				</div>
				<div class="form-group">
					<label class="col-lg-4 control-label">Enable</label>
					<div class="col-lg-1">
						<input type="checkbox" name="glbu_enabled" class="i-checks" checked> 
					</div>
				</div>
			</form>
		</div>
	</div>

	<script type="text/javascript">
	$(document).ready(function() {

		$('#glbuForm').data('serialize',$('#glbuForm').serialize());
        function alert_cancel(){
                if($('#glbuForm').serialize()!=$('#glbuForm').data('serialize')){
                // Form has changed!!!
                swal({
                  title: "Are you sure?",
                  text: "Your work will not be save",
                  type: "warning",
                  showCancelButton: true,
                  confirmButtonColor: "#DD6B55",
                  confirmButtonText: "Leave",
                  cancelButtonText: "Stay",
                  closeOnConfirm: true,
                  closeOnCancel: true },
              function (isConfirm) {
                  if (isConfirm) {
                    swal("success");
                      close_add(); active_search();
                      notifyClass("clear","");
                  } else {
                      swal("Cancelled");
                  }
              });
                }else{
                    close_add(); active_search();
                    notifymsg("hide","hide");
                }
            }
        $('#cancel_add').click(function(){
            alert_cancel();
        });

        $('#addX').click(function(){
            alert_cancel();        
        });

		$('.chosen-select').chosen({ width: '100%' });

		$('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green'
        });

        $('#save_glbusinessunit').click(function(){
            if($("#glbuForm" ).valid() == true){
                $.ajax({
	                url:"<?php echo base_url($this->session->userdata('forajax').'/add_bu'); ?>",
	                method:'post',
	                data:$('#glbuForm').serialize(),
	                dataType:'json',
	                success: function (data) {	                
	                	if(data.err == 1){
                        	notifymsg('danger','<b>Error</b>: ' + data.msg);
                        	$('#save_glbusinessunit').removeAttr('disabled');
                        }else{
                        	notifymsg('success','<b>Success</b>: Data saved.');
                        	$('#close-add-body').parents('li').remove('li');
				            $('#add').remove();
				            $('#tab-list-2 a:first').tab('show');
				            $('#count_add').val('0');
                        }
	                },
	                beforeSend: function(){
	                	$('#add_loading_icon').show();
	                	$('#save_glbusinessunit').attr('disabled',true);
	                },
	                complete: function(){
	                	$('#add_loading_icon').hide();
	                },
	                error: function(xhr){
	                	notifymsg('danger','<b>Error</b>: ' + xhr.responseText);
	                }
	            });
            }
        });

		$.validator.setDefaults({ ignore: ":hidden:not(select)" });
        $("#glbuForm").validate({
                 rules: {
                     bu_name: {
                        required: true
                     },
                     bu_code: {
                     	required : true
                     },
                     bu_cogs: {
                        required: true
                     },
                     bu_sales: {
                        required: true
                     },
                     bu_exp: {
                        required: true
                     },
                     bu_company: {
                        required: true
                     }
                 }
        });
	});
	</script>